//
//  ProfileViewController.swift
//  MyTest
//
//  Created by guanzhongwang on 2021/4/1.
//

import UIKit

class ProfileViewController: CommonViewController {
    private let scroll = UIScrollView()
    private lazy var loginButton:UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitle("登录", for: UIControl.State.normal)
        btn.setBackgroundImage(RED_COLOR.toImage(), for: .normal)
        scroll.addSubview(btn)
        btn.snp.makeConstraints({
            $0.centerX.equalToSuperview()
            $0.top.equalTo(50)
            $0.width.height.equalTo(80)
        })
        btn.layer.cornerRadius = 40
        btn.clipsToBounds = true
        btn.addTarget(self, action: #selector(loginClick), for: .touchUpInside)
        return btn
    }()
    
    private lazy var userInfoView:UIView = {
        let view = TopUserInfoView()
        scroll.addSubview(view)
        view.snp.makeConstraints({
            $0.top.equalTo(15+navigationBarHeight)
            $0.left.equalTo(0)
            $0.width.equalToSuperview()
        })
        return view
    }()
    private let navigationView = UIView()
    private let descView = ProfilePushView()
    private let mangerView =  ManagerView()
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
     
        
        view.addSubview(scroll)
        scroll.delegate = self
        scroll.showsVerticalScrollIndicator = false
        scroll.snp.makeConstraints({
            $0.top.equalTo(0)
            $0.left.bottom.right.equalToSuperview()
            $0.width.equalTo(ScreenWidth)
        })
        
        let setitem = UIButton(type: .custom)
        setitem.setImage(UIImage(named: "setting"), for: .normal)
        setitem.addTarget(self, action: #selector(setting), for: .touchUpInside)
        navigationView.addSubview(setitem)
        setitem.snp.makeConstraints({
            $0.right.equalToSuperview().inset(15)
            $0.width.equalTo(20)
            $0.top.equalTo(UIApplication.shared.statusBarFrame.height)
            $0.bottom.equalToSuperview()
        })
        let mailitem = UIButton(type: .custom)
        mailitem.setImage(UIImage(named: "mail"), for: .normal)
        mailitem.addTarget(self, action: #selector(chatlist), for: .touchUpInside)
        navigationView.addSubview(mailitem)
        mailitem.snp.makeConstraints({
            $0.right.equalTo(setitem.snp.left).offset(-15)
            $0.width.equalTo(20)
            $0.top.equalTo(UIApplication.shared.statusBarFrame.height)
            $0.bottom.equalToSuperview()
        })
        let qritem = UIButton(type: .custom)
        qritem.setImage(UIImage(named: "QR"), for: .normal)
        qritem.addTarget(self, action: #selector(qr), for: .touchUpInside)
        navigationView.addSubview(qritem)
        qritem.snp.makeConstraints({
            $0.left.equalTo(15)
            $0.width.equalTo(20)
            $0.top.equalTo(UIApplication.shared.statusBarFrame.height)
            $0.bottom.equalToSuperview()
        })
        mangerView.delegate = self
        scroll.addSubview(mangerView)
        mangerView.snp.makeConstraints({
            $0.left.equalTo(15)
            $0.width.equalTo(ScreenWidth-30)
            if(DataManager.login){
                $0.top.equalTo(userInfoView.snp.bottom)
            }else{
                $0.top.equalTo(loginButton.snp.bottom).offset(60)
            }
        })
        
        scroll.addSubview(descView)
        descView.snp.makeConstraints({
            $0.left.equalTo(15)
            $0.width.equalTo(ScreenWidth-30)
            $0.top.equalTo(mangerView.snp.bottom).offset(10)
        })
        
        let moreServiceView = UIView()
        moreServiceView.backgroundColor = .white
        scroll.addSubview(moreServiceView)
        moreServiceView.snp.makeConstraints({
            $0.left.width.equalTo(mangerView)
            $0.top.equalTo(descView.snp.bottom).offset(10)
            $0.bottom.equalTo(scroll).inset(15)
        })
        let tipLabel = UILabel()
        moreServiceView.addSubview(tipLabel)
        tipLabel.text = "更多服务"
        tipLabel.font = getLabelFont(15,blod: true)

        tipLabel.snp.makeConstraints({
            $0.left.equalTo(15)
            $0.top.equalTo(15)
        })
        
        let serviceView =  ServiceView()
        serviceView.delegate = self
        moreServiceView.addSubview(serviceView)
        
        serviceView.setMenus(menus: [ProfileMenuModel.init(title: "用户反馈", imageName: "feedback", className: ""),ProfileMenuModel.init(title: "钱包", imageName: "qianbao"),ProfileMenuModel.init(title: "广告推广", imageName: "guanggao"),ProfileMenuModel.init(title: "免流量服务", imageName: "liuliang"),ProfileMenuModel.init(title: "评论", imageName: "pinglunmenu", className: "StoreViewController"),ProfileMenuModel.init(title: "点赞", imageName: "zan",className: "StoreViewController"),ProfileMenuModel.init(title: "夜间模式", imageName: "yejianmoshi"),ProfileMenuModel.init(title: "隐私设置", imageName: "yinsishezhi",className: "PrivacSetViewController"),ProfileMenuModel.init(title: "订单", imageName: "ordermenu", className: ""),ProfileMenuModel.init(title: "购物车", imageName: "gouwuche"),ProfileMenuModel.init(title: "用户反馈", imageName: "feedback", className: ""),ProfileMenuModel.init(title: "钱包", imageName: "qianbao"),ProfileMenuModel.init(title: "广告推广", imageName: "guanggao"),ProfileMenuModel.init(title: "免流量服务", imageName: "liuliang"),ProfileMenuModel.init(title: "评论", imageName: "pinglunmenu", className: "StoreViewController"),ProfileMenuModel.init(title: "点赞", imageName: "zan",className: "StoreViewController"),ProfileMenuModel.init(title: "夜间模式", imageName: "yejianmoshi"),ProfileMenuModel.init(title: "隐私设置", imageName: "yinsishezhi",className: "PrivacSetViewController"),ProfileMenuModel.init(title: "订单", imageName: "ordermenu", className: ""),ProfileMenuModel.init(title: "购物车", imageName: "gouwuche"),ProfileMenuModel.init(title: "用户反馈", imageName: "feedback", className: ""),ProfileMenuModel.init(title: "钱包", imageName: "qianbao"),ProfileMenuModel.init(title: "广告推广", imageName: "guanggao"),ProfileMenuModel.init(title: "免流量服务", imageName: "liuliang"),ProfileMenuModel.init(title: "评论", imageName: "pinglunmenu", className: "StoreViewController"),ProfileMenuModel.init(title: "点赞", imageName: "zan",className: "StoreViewController"),ProfileMenuModel.init(title: "夜间模式", imageName: "yejianmoshi"),ProfileMenuModel.init(title: "隐私设置", imageName: "yinsishezhi",className: "PrivacSetViewController"),ProfileMenuModel.init(title: "订单", imageName: "ordermenu", className: ""),ProfileMenuModel.init(title: "购物车", imageName: "gouwuche")])
        serviceView.snp.makeConstraints({
            $0.left.equalTo(0)
            $0.width.equalTo(ScreenWidth-30)
            $0.top.equalTo(tipLabel.snp.bottom).offset(15)
            $0.bottom.equalToSuperview()
        })
        
        NotificationMananger.addNotification(.LoginCompletion, target: self, selector: #selector(updateData))
        self.navigationController?.delegate = self
        
        view.addSubview(navigationView)
        navigationView.snp.makeConstraints({
            $0.height.equalTo(navigationBarHeight)
            $0.top.left.right.equalTo(0)
        })
        navigationView.backgroundColor = .red
        navigationView.backgroundColor = UIColor(red: 1, green: 1, blue: 1, alpha: 0)
    }
    
    @objc func updateData() {
        self.userInfoView.isHidden = !DataManager.login
        self.loginButton.isHidden = DataManager.login
        self.descView.createUI()
        self.mangerView.snp.remakeConstraints({
            $0.left.equalTo(15)
            $0.width.equalTo(ScreenWidth-30)
            if(DataManager.login){
                $0.top.equalTo(self.userInfoView.snp.bottom)
            }else{
                $0.top.equalTo(self.loginButton.snp.bottom).offset(60)
            }
        })
    }
    
    @objc func setting(){
        self.pushViewController(SettingViewController(), animated: true,hidesBottomBar: true)
    }
    @objc func chatlist(){
        self.pushViewController(ChatListViewController(), animated: true,hidesBottomBar: true)
    }
    @objc func qr(){
        
    }
 
    @objc func loginClick(){
        let vc = LoginViewController()
        let nav = UINavigationController.init(rootViewController: vc)
        nav.modalPresentationStyle = .overFullScreen
        self.present(nav, animated: true) {
            
        }
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)


    }
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        scroll.setContentOffset(CGPoint.init(x: 0, y: 0), animated: false)
    }

}
extension ProfileViewController:UINavigationControllerDelegate{
    func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
        let res = viewController == self
        self.navigationController?.setNavigationBarHidden(res, animated: true)
    }
}
extension ProfileViewController:UIScrollViewDelegate{
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        var delta = scrollView.contentOffset.y / CGFloat(navigationBarHeight*2)
        delta = CGFloat.maximum(delta, 0)
        SystemLog(delta)
        self.navigationView.backgroundColor = UIColor.init(red: 1, green: 1, blue: 1, alpha: delta)
    }
}
extension ProfileViewController:MenuServiceDelegate{

    func itemSelected(className: String,title:String) {
        if className != "" {
            let vc = String.ClassFromString(className)
            vc?.title = title
            self.pushViewController(vc! , animated: true)
            self.hidesBottomBarWhenPushed = false
        }
    }
}
